﻿using Cinemachine;
using UnityEngine;
using UnityEngine.UI;
using TouchInputManager;

namespace TouchInputManager.Tests
{
    /// <summary>
    /// 子场景默认会通过rtt到ui中进行展示与控制。 
    /// 同时需要能够直接运行方便调试与测试效果。
    /// </summary>
    public class RTTSubSceneCtr : MonoBehaviour
    {
        public CinemachineBrain CmBrain { get; protected set; }

        public Button merge;
        private StructureDisperseAnimPlayer animPlayer;
        protected virtual float GetInputAxisHandle(string axisName)
        {
#if TOUCHMODEL
            if (axisName != "Multi Finger" && axisName != "Mouse X" && axisName != "Mouse Y")
                return 0;
            return TouchManager.Instance.GetTouchInput(axisName);
#else

            if ((axisName == "Mouse X" || axisName == "Mouse Y") && !Input.GetMouseButton(0))
                return 0;

            return Input.GetAxis(axisName);
#endif
        }

        private void Update()
        {
            TouchManager.Instance.ClickCheck();
        }

        protected virtual void OnDestroy()
        {
            if (CinemachineCore.GetInputAxis == GetInputAxisHandle)
                CinemachineCore.GetInputAxis = Input.GetAxis;
        }

        public bool isDisperse = false;

        public Text click;
        public Text doubleClick;
        public Text longPress;

        public int clickCount;
        public int doubleClickCoun;
        public int longPressCoun;

        protected virtual void Start()
        {
            if (CinemachineCore.Instance.BrainCount > 0)
            {
                CmBrain = CinemachineCore.Instance.GetActiveBrain(0);

                //方便直接运行场景测试，镜头输入在此绑定
                CinemachineCore.GetInputAxis = GetInputAxisHandle;
            }

            TouchManager.Instance.clickTouch.OnClickEvent += (obj) =>
            {
                clickCount += 1;
                click.text = clickCount.ToString();
            };

            TouchManager.Instance.clickTouch.OnDoubleEvent += (obj) =>
            {
                doubleClickCoun += 1;
                doubleClick.text = doubleClickCoun.ToString();

                animPlayer = (obj as Component).GetComponent<StructureDisperseAnimPlayer>();

                isDisperse = !isDisperse;

                if (isDisperse)
                {
                    StartCoroutine(animPlayer.PlayDisperseEffect());
                }
                else
                {
                    StartCoroutine(animPlayer.PlayMergeEffect());
                }
            };

            TouchManager.Instance.clickTouch.OnLongPressEvent += (obj) =>
            {
                longPressCoun += 1;
                longPress.text = longPressCoun.ToString();
            };

            merge.onClick.AddListener(()=> {

                if (animPlayer == null) return;

                isDisperse = !isDisperse;

                if (isDisperse)
                {
                    StartCoroutine(animPlayer.PlayDisperseEffect());
                }
                else
                {
                    StartCoroutine(animPlayer.PlayMergeEffect());
                }
            });
        }
    }
}
